package com.baitengsoft.patentapimonitor.api.service.impl;

import com.baitengsoft.patentapimonitor.api.dao.CfgDictionaryDao;
import com.baitengsoft.patentapimonitor.api.entity.CfgDictionaryEntity;
import com.baitengsoft.patentapimonitor.api.model.CfgDictionaryInfo;
import com.baitengsoft.patentapimonitor.api.service.CfgDictionaryService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * Created by Admin on 2018/1/8.
 */
@Service
public class CfgDictionaryServiceImpl extends BaseServiceImpl<CfgDictionaryEntity> implements CfgDictionaryService {

    @Autowired
    private CfgDictionaryDao cfgDictionaryDao;
    @Override
    public List<CfgDictionaryEntity> searchDictionaryByCode(String code) {
        if(StringUtils.isNotBlank(code)){
//            CfgDictionaryEntity entity=new CfgDictionaryEntity();
//            entity.setCode(code);
//            List<CfgDictionaryEntity> entities=queryListByWhere(entity);
            List<CfgDictionaryEntity> entities=cfgDictionaryDao.searchDictionaryByCode(code);
            if(entities!=null&&entities.size()>0) return entities;
        }
        return null;
    }

    /**
     * 根据code只获取Name和Value，【下拉框】
     */
    @Override
    public List<CfgDictionaryInfo> getNameAndValueByCode(String code) {
        if (StringUtils.isEmpty(code)){
            return new ArrayList<>();
        }
        List<CfgDictionaryEntity> entities=cfgDictionaryDao.searchDictionaryByCode(code);
        List<CfgDictionaryInfo> infos = new ArrayList<>();
        for(CfgDictionaryEntity entity :entities){
            CfgDictionaryInfo info = new CfgDictionaryInfo();
            info.setName(entity.getName());
            info.setValue(entity.getValues());
            infos.add(info);
        }
        return infos;
    }

    /**
     * 根据名字和编码查询value
     */
    @Override
    public String getValueByCodeAndName(String code, String name) {
        if (StringUtils.isEmpty(code)) return "";
       return cfgDictionaryDao.getValueByCodeAndName(code,name);
    }

    /**
     * 根据value和Code获取name
     */
    @Override
    public String getNameByCodeAndValue(String code, String value) {
        if (StringUtils.isEmpty(code)) return "";
        String nameByCodeAndValue = cfgDictionaryDao.getNameByCodeAndValue(code, value);
        return nameByCodeAndValue;
    }

    @Override
    public Map<String, String> getPatTypeMap() {
        List<CfgDictionaryInfo> patType = getNameAndValueByCode("1003");
        Map<String, String> map = new HashMap<>();
        for (CfgDictionaryInfo info : patType){
            map.put(info.getValue(),info.getName());
        }
        return map;
    }

    @Override
    public Map<String, String> getLowStatus() {
        List<CfgDictionaryInfo> patType = getNameAndValueByCode("1001");
        Map<String, String> map = new HashMap<>();
        for (CfgDictionaryInfo info : patType){
            map.put(info.getValue(),info.getName());
        }
        return map;
    }

    @Override
    public Map<String, String> getInvoidLowStatus() {
        Map<String, String> lowStatus = getLowStatus();
        Map<String,String> result = new LinkedHashMap<>();
        String[] invoidValue = {"4","6","7","8","9","10","11","12","13","15"};
        List<String> invoidValueList  = Arrays.asList(invoidValue);
        for(String value :invoidValueList){
            if (StringUtils.isNotBlank(lowStatus.get(value))){
                result.put(value,lowStatus.get(value));
            }
        }
        return result;
    }

    @Override
    public Map<String, String> getApplyPersonType() {
        List<CfgDictionaryInfo> patType = getNameAndValueByCode("1004");
        Map<String, String> map = new LinkedHashMap<>();
        for (CfgDictionaryInfo info : patType){
            map.put(info.getValue(),info.getName());
        }
        return map;
    }

}
